<template>
  <div>
    <div style="width: 50%;margin: 0 auto;margin-top: 100px;">
      <el-card >
        <h1> 注册页面</h1>
        <el-form :model="user"  ref="user"  :rules="rules" label-width="80px" style="width: 700px;margin: 0 auto;">
          <el-form-item label="用户名" prop="username">
            <el-input style="width: 600px;" placeholder="请输入用户名" v-model="user.username"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" style="width: 600px;" placeholder="请输入用户名" v-model="user.password"/>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input style="width: 600px;" placeholder="请输入用户名" v-model="user.nickname"/>
          </el-form-item>
          <el-form-item >
           <el-button type="primary" @click="reg()">注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {
        username:'',
        password:'',
        nickname:''
      },
      rules:{
        username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      }
    }

  },
  methods: {
    reg() {
      let url = this.BASE_URL + '/v1/users/reg'
      this.axios.post(url,this.user).then(response=>{
        if(response.data.code==10001){
          this.$message.success("注册成功")
          this.$router.push("/login")
        }else {
          this.$message.error(response.data.msg)
        }
      })
    }
  },
}
</script>

<style scoped>

</style>
